package com.qiangesoft.cloud.seata.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.cloud.seata.feign.StockFeignClient;
import com.qiangesoft.cloud.seata.mapper.OrderMapper;
import com.qiangesoft.cloud.seata.model.Order;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private StockFeignClient stockFeignClient;

    @GlobalTransactional
    public int saveOrder() {
        // 入库订单
        Order order = new Order();
        order.setProductId("987654321");
        order.setPrice(199.00);
        order.setCreateTime(LocalDateTime.now());
        baseMapper.insert(order);

        // 库存扣减

//        ResponseEntity<Integer> responseEntity = restTemplate.postForEntity("http://seata-stock-service/stock/decNum", order, Integer.class);
        return stockFeignClient.decNum(order);
    }

}
